package org.javaboy.dynamic_datasource.ds;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import javax.xml.crypto.Data;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

@Configuration
@EnableConfigurationProperties(DataSourceProperties.class)
public class YamlDynamicDataSourceProvider implements DynamicDataSourceProvider {
    DataSourceProperties dataSourceProperties;

    public YamlDynamicDataSourceProvider(DataSourceProperties dataSourceProperties) {
        this.dataSourceProperties = dataSourceProperties;
    }

    @Override
    public Map<String, DataSource> loadAllDataSource() {
        Map<String, DataSource> map = new HashMap<>();
        try {
            String driverClassName = dataSourceProperties.getDriverClassName();
            String type = dataSourceProperties.getType();
            Map<String, Map<String, String>> ds = dataSourceProperties.getDs();
            Set<String> keyedSet = ds.keySet();
            for (String key : keyedSet) {
                Map<String, String> dsProperties = ds.get(key);
                DataSource dataSource = DruidDataSourceFactory.createDataSource(dsProperties);
                map.put(key, dataSource);
            }
        } catch (Exception e) {
//            throw new RuntimeException(e);
        }
        return map;
    }
}
